//活动号管理.js


function show_WdatePicker(th){
	   $(th).prev().show();
}
function setproductdate(th){
	layer.confirm("确定设置此日期？",function(){
		var expiryTime = $("#expiryTime").val();
		var id=$("#prod_id").val();
	    var url=path+"/ticket_coupon/set_ticket_coupon_product_down_date";
	    layer.closeAll('dialog');
	    var layer_load_window = layer.load(2); 
		 $.ajax({
				url : url,
				type : "post",
				dataType : "text",
				data : {
					"date":expiryTime,
					"id":id,
				},
				success : function(msg){
				 if(msg == "success"){
		   			 layer.msg('设置成功!',{icon:1,time:1000},function(){
		   				 $("#query_active_list_form").submit();
		   			 });
		   		  }else if(msg == "failone"){
		   			layer.alert("设置失败,设置的过期日期不能早于当前日期或晚于有效期");
		   		  }else{
		   			layer.alert("设置失败");
		   		  }
		   	  },
			  error : function(){
				  layer.alert("操作失败");
			  },complete: function(XMLHttpRequest, textStatus) {
					layer.close(layer_load_window);
			  }
			});
	});
}
/**添加活动号*/
function addActive(){
	var tr = "<tr><td><input  type='text'  style='width:100px;' class='input-text  radius' placeholder='请输入' id='active_no'/><a href='javascript:void(0);' onclick='checkMileage()'>查询</a></td><td><select id='mileage_select' style='display:none;' name='xxx'></select></td><td><input type='text'  class='input-text Wdate'  id='month' onclick=\"WdatePicker({dateFmt:'yyyy-MM'})\",readOnly: true, size='20' /></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>";
	$("#coupon_table tbody").prepend(tr);
		//换成保存和取消两个按钮
		var html = "<input  class='btn btn-success radius'  type='button' id='save' value='保存' onclick='saveActive()' class='form_btn'/><input type='button' value='取消'  class='btn btn-secondary radius'  id='cancel' name='cancel'  class='form_btn' style='margin-left:20px;'/>";
		$("#operate").html(html);
		$("#cancel").click(function(){
			$("#coupon_table tr:eq(1)").remove();
			var html="<input  class='btn btn-success radius'  type='button' id='addActive' value='添加活动号' onclick='addActive()' name='add' class='form_btn' style='margin-top:20px;'/>";
			$("#operate").html(html);
		});
}
/***查询活动号的面值***/
function checkMileage(){
	var activeNo = $("#active_no").val().trim();
	if(activeNo != ""){
		layer.closeAll('dialog');
		var layer_load_window = layer.load(2); 
		var param = {"activeNo":activeNo};
		$.ajax({
		url:path+"/ticket_coupon/query_face_values",
		type:"post",
		data:param,
		dataType:"json",
		error:function(XMLHttpRequest, textStatus, errorThrown){
			layer.alert(errorThrown);
		},
		success:function(e){
			var result = e;
			if(result.success==false){
				layer.alert(result.message);
			}else{
				var faceValues = result.faceValue;
				var option = "";
				for(var i = 0; i<faceValues.length;i++){
					option = option+"<option value='"+faceValues[i]+"'>"+faceValues[i]+"</option>";
				}
			}
			$("#mileage_select").empty();
			$("#mileage_select").append(option);
			$("#mileage_select").show();
		},complete: function(XMLHttpRequest, textStatus) {
			layer.close(layer_load_window);
		}
	});
	}else{
		layer.alert("请选输入活动号");
	}	
}

/**添加活动**/
function saveActive(){
	//活动ID
	var active_no = $("#active_no").val().trim();
	//面值
	var mileage = $("#mileage_select").val().trim();
	//月份
	var month = $("#month").val().trim();
	var prod_id = $("#prod_id").val().trim();
	
	if(active_no == ""){
		layer.alert("请输入活动号");
		return;
	}
	if(mileage == ""){
		layer.alert("请选择面值");
		return;
	}
	if(month == ""){
		layer.alert("请选择活动月份");
		return ;
	}
	if(prod_id == ""){
		layer.alert("请选择商品");
		return ;
	}
		var param = {"activeNo":active_no,"ticketMileage":mileage,"active_month":month,"prodId":prod_id};
		layer.closeAll('dialog');
		var layer_load_window = layer.load(2); 
		$.ajax({
			url:path+"/ticket_coupon/create_active",
			type:"post",
			data: param,
			dataType:"json",
			error:function(XMLHttpRequest, textStatus, errorThrown){
				layer.alert(errorThrown);
			},
			success:function(result){
				if(result.succ==false){
					layer.alert(result.message);
				}else{
					layer.msg(result.message,{icon:1,time:1000},function(){
						  $("#query_active_list_form").submit();
					});	
				}
			},complete: function(XMLHttpRequest, textStatus) {
				layer.close(layer_load_window);
			}
		});
}


//同步库存
function synchroStock(activeId){
	var prodId = $("#prod_id").val();
	var param  = {"prodId":prodId,"activeId":activeId};
	layer.closeAll('dialog');
	var layer_load_window = layer.load(2); 
	$.ajax({
		url:path+"/ticket_coupon/synchro_stock",
		type:"post",
		dataType:"json",
		data:param,
		success:function(e){
			if(typeof(e) == undefined){
			}else{
				layer.msg(e.message,{icon:1,time:1000},function(){
					    $("#query_active_list_form").submit();
				});
			}
		},error:function(XMLHttpRequest, textStatus, errorThrown){
			layer.alert("操作失败!");
		},complete: function(XMLHttpRequest, textStatus) {
			layer.close(layer_load_window);
		}
	});
}
//作废活动
function invalidate(id){
	layer.confirm("注意：作废操作后将无法恢复 请确认是否要作废",function(){
	var activeId  = id;
	var prodId = $("#prod_id").val();
	if(activeId==null||activeId==""){
		layer.alert("请选择活动");
	}else{
		var param = {"activeId":activeId};
		$.ajax({
			url:path+"/ticket_coupon/invalidate_active",
			type:"post",
			data:param,
			dataType:"json",
			error:function(XMLHttpRequest, textStatus, errorThrown){
				layer.alert("操作失败!");
			},
			success:function(e){
				layer.msg(e.message,{icon:1,time:1000},function(){
					    $("#query_active_list_form").submit();
				});
			}
		});
	}
	});
}